package com.sino.androiddesign.design

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.BottomSheetBehavior
import android.support.design.widget.BottomSheetDialog
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.*
import android.widget.Toast
import com.sino.androiddesign.R
import com.sino.androiddesign.recyclerviewdemo.BaseRecyclerAdapter
import com.sino.androiddesign.recyclerviewdemo.BaseRecyclerHolder
import com.sino.androiddesign.recyclerviewdemo.Bean
import com.sino.androiddesign.recyclerviewdemo.UniversalAdapterFragment
import kotlinx.android.synthetic.main.activity_tool_bar.*

/**
 * 在theme隐藏actionBar有两种方式
 * 1: 在style的item中 添加
 * <item name="windowActionBar">false</item>
 * <item name="android:windowNoTitle">true</item>
 * 2:style 继承 Theme.AppCompat.Light.NoActionBar
 *
 * toolbar 中的logo title,subTitle,navigationIcon ...都是app属性
 *
 * Options Menu
 * 与actionBar的用法一致,在menu资源文件下新建一个.xml文件
 * <menu xmlns:android="http://schemas.android.com/apk/res/android"
 *   xmlns:app="http://schemas.android.com/apk/res-auto">
 *    <item
 *       android:id="@+id/action_search"
 *       android:icon="@android:drawable/ic_menu_search"
 *        android:title="search"
 *        // showAsAction: always 总是显示
 *        //never 不显示,只出现在右边三个点中
 *        //ifRoom 如果有位置才显示不然就隐藏在三个点中
 *         app:showAsAction="collapseActionView" />
 *</menu>
 *
 * 修改menu的展示方式 参考 ToolBarStyle
 */
class ToolbarActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tool_bar)

        setSupportActionBar(toolbar_tl)

        toolbar_amv.setOnMenuItemClickListener {
            Toast.makeText(this, "你点击了${it.title}", Toast.LENGTH_SHORT).show()

            true
        }

        toolbar_bottom_btn.setOnClickListener {

            /**
             * 底部弹出框
             */
            var view = LayoutInflater.from(this).inflate(R.layout.activity_tool_bar_bottom, null)

            var recyclerView = view.findViewById<RecyclerView>(R.id.tool_bar_bottom_rv)
            recyclerView.layoutManager = LinearLayoutManager(this)
            var beans = ArrayList<Bean>()
            for (i in 0 until 10) {
                beans.add(Bean("test$i", R.mipmap.ic_launcher))
            }
            recyclerView.adapter = object : BaseRecyclerAdapter<Bean>(this, beans, R.layout.fragment_universal_adapter_item) {
                override fun convert(holder: BaseRecyclerHolder, item: Bean, position: Int) {
                    holder.setText(R.id.universal_item_tv, item.text)
                    holder.setImage(R.id.universal_item_iv, item.img)
                }
            }

            var bottomSheetDialog = BottomSheetDialog(this)
            bottomSheetDialog.setContentView(view)
            bottomSheetDialog.setCancelable(true)
            bottomSheetDialog.setCanceledOnTouchOutside(true)
            bottomSheetDialog.show()
        }

        var shareDialog: BottomSheetDialog? = null
        toolbar_share_btn.setOnClickListener {
            /**
             * 实例化一次 多次调用
             */
            if (shareDialog == null) {
                var view = LayoutInflater.from(this).inflate(R.layout.activity_tool_bar_bottom, null)

                var recyclerView = view.findViewById<RecyclerView>(R.id.tool_bar_bottom_rv)
                recyclerView.layoutManager = LinearLayoutManager(this)
                var beans = ArrayList<Bean>()
                for (i in 0 until 4) {
                    beans.add(Bean("test$i", R.mipmap.ic_launcher))
                }
                recyclerView.adapter = object : BaseRecyclerAdapter<Bean>(this, beans, R.layout.fragment_universal_adapter_item) {
                    override fun convert(holder: BaseRecyclerHolder, item: Bean, position: Int) {
                        holder.setText(R.id.universal_item_tv, item.text)
                        holder.setImage(R.id.universal_item_iv, item.img)
                    }
                }

                shareDialog = BottomSheetDialog(this)
                shareDialog!!.setContentView(view)
                shareDialog!!.setCancelable(true)
                shareDialog!!.setCanceledOnTouchOutside(true)
                shareDialog!!.show()

                /**
                 * 可以获取到整个 bottomDialog 的view
                 */
                var bottomView = shareDialog!!.delegate.findViewById<View>(android.support.design.R.id.design_bottom_sheet)
                bottomView!!.setBackgroundColor(Color.GREEN)
                var sheetBehavior = BottomSheetBehavior.from(bottomView)
                sheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
                    override fun onSlide(bottomSheet: View, slideOffset: Float) {

                    }

                    /**
                     * 底部dialog 的改变
                     */
                    override fun onStateChanged(bottomSheet: View, newState: Int) {
                        if (newState == BottomSheetBehavior.STATE_HIDDEN){
                            shareDialog!!.dismiss()
                            sheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
                        }
                    }
                })

            } else {
                println("123")
                shareDialog!!.show()
            }
        }
    }

    /**
     * 创建操作menu
     */
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.search, menu)
        menuInflater.inflate(R.menu.action_menu, toolbar_amv.menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item!!.itemId) {
            R.id.action_search -> {
                Toast.makeText(this, "你点击了${item.title}", Toast.LENGTH_SHORT).show()
            }
        }
        return super.onOptionsItemSelected(item)
    }
}
